#include "reservation_record.h"
#include "ui_reservation_record.h"
#include <QCalendarWidget>
#include <QFuture>
#include <QVariant>
#include "log/log_init.h"
#include <QtConcurrent>
ReservationRecord::ReservationRecord(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReservationRecord)
{
    ui->setupUi(this);
    model = new QSqlQueryModel(this/*,ContrlLogic::getInstance()->getDataDBClient()*/);
    model->setQuery(QString(DEFAULT_SQL_1 + QString(" order by order_create_time desc ")));

    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    model->setHeaderData(0,Qt::Horizontal,"部门");
    model->setHeaderData(1,Qt::Horizontal,"创建人员");
    model->setHeaderData(2,Qt::Horizontal,"order_id");
    model->setHeaderData(3,Qt::Horizontal,"创建者id");
    //model->setHeaderData(3,Qt::Horizontal,"订单状态");
    model->setHeaderData(4,Qt::Horizontal,"创建时间");
    model->setHeaderData(5,Qt::Horizontal,"更新时间");
    model->setHeaderData(6,Qt::Horizontal,"删除时间");
    model->setHeaderData(7,Qt::Horizontal,"客户称谓");
    model->setHeaderData(8,Qt::Horizontal,"会员等级");
    model->setHeaderData(9,Qt::Horizontal,"电话号码");
    model->setHeaderData(10,Qt::Horizontal,"消费偏向");
    model->setHeaderData(11,Qt::Horizontal,"特殊备注");
    model->setHeaderData(12,Qt::Horizontal,"订台时间");
    model->setHeaderData(13,Qt::Horizontal,"预计到场时间");

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//是否可以编辑
    //ui->tableView->setColumnHidden(0,1);
    ui->tableView->setColumnHidden(2,1);
    ui->tableView->setColumnHidden(3,1);
    ui->tableView->setColumnHidden(4,1);
    //ui->tableView->setColumnHidden(6,1);
    ui->tableView->setColumnHidden(6,1);
    ui->tableView->setStyleSheet("QHeaderView::section { background-color: rgb(255, 186, 67) }");
    setTimeEdit();
    lastQuery = DEFAULT_SQL_1;
    QList<QList<QString>> data =  DBManager::getInstance()->select(GET_VIP_LEVEL);
    for(auto it :data){
        ui->vip_comboBox->addItem(it.at(0));
    }
}

ReservationRecord::~ReservationRecord()
{
    delete ui;
}

void ReservationRecord::export_excel()
{
    exportExcle.Table2ExcelByHtml(ui->tableView,"sosplus");
}

void ReservationRecord::on_export_btn_clicked()
{
    export_excel();
//    QFuture<void> future = QtConcurrent::run(this, &ReservationRecord::export_excel);
//    while(!future.isFinished())
//    {
//        QApplication::processEvents(QEventLoop::AllEvents, 100);
//    }
}

void ReservationRecord::on_update_btn_clicked()
{
    model->setQuery(QString(DEFAULT_SQL_1)+ QString(" order by order_create_time desc "));
    lastQuery = DEFAULT_SQL_1 + QString(" order by order_create_time desc ");
    ui->vipLv_check->setChecked(0);
    ui->sureuser_check->setChecked(0);
    ui->time_check->setChecked(0);
}

void ReservationRecord::setTimeEdit()
{
    QCalendarWidget *m_pCalendarWidget;
    m_pCalendarWidget = new QCalendarWidget(this);
    m_pCalendarWidget->setMinimumDate(QDate(2020,1,1));
    m_pCalendarWidget->setFixedSize(300,200); //日历控件的显示大小
    m_pCalendarWidget->show();

    ui->start_time->setCalendarPopup(true); //使用该句可以直接调用日历控件
    ui->start_time->setCalendarWidget(m_pCalendarWidget);
    ui->start_time->setDisplayFormat("yyyy/MM/dd");//yyyy/MM/dd
    ui->start_time->setDate(m_pCalendarWidget->selectedDate());
    ui->start_time->setDateRange(QDate(2020,1,1),
                                 m_pCalendarWidget->maximumDate());

    QCalendarWidget *m_pCalendarWidget_end;
    m_pCalendarWidget_end = new QCalendarWidget(this);
    m_pCalendarWidget_end->setMinimumDate(QDate(2020,1,1));
    m_pCalendarWidget_end->setFixedSize(300,200); //日历控件的显示大小
    m_pCalendarWidget_end->show();
    ui->end_time->setCalendarPopup(true); //使用该句可以直接调用日历控件
    ui->end_time->setCalendarWidget(m_pCalendarWidget_end);
    ui->end_time->setDisplayFormat("yyyy/MM/dd");//yyyy/MM/dd
    ui->end_time->setDate(m_pCalendarWidget_end->selectedDate());
    ui->end_time->setDateRange(QDate(2021,1,1),
                               m_pCalendarWidget_end->maximumDate());
    QDateTime current_date_time = QDateTime::currentDateTime();
    ui->end_time->setDate(current_date_time.date());

}

void ReservationRecord::on_select_clicked()
{
    QString query = "";

    if(ui->vipLv_check->isChecked()&&ui->vip_comboBox->count()>0){
        query += "cmf_orders.order_vip_level";
        query += " = ";
        query +="'";
        query += ui->vip_comboBox->currentText().simplified();
        query +="'";
    }
    if(ui->sureuser_check->isChecked()){
        if(ui->vipLv_check->isChecked())
            query += " and ";
        query += "cmf_user.user_nickname";
        query += " = ";
        query +="'";
        query += ui->sureuser_lineEdit->text().simplified();
        query +="'";
    }
    if(ui->time_check->isChecked()){
        if(ui->sureuser_check->isChecked()||(ui->vipLv_check->isChecked()&&!ui->sureuser_check->isChecked()))
            query += " and ";
        QString startYear = ui->start_time->sectionText(QDateTimeEdit::YearSection);
        QString startMonth = ui->start_time->sectionText(QDateTimeEdit::MonthSection);
        QString startDay = ui->start_time->sectionText(QDateTimeEdit::DaySection);

        QString endYear = ui->end_time->sectionText(QDateTimeEdit::YearSection);
        QString endMonth = ui->end_time->sectionText(QDateTimeEdit::MonthSection);
        QString endDay = ui->end_time->sectionText(QDateTimeEdit::DaySection);

        query += "cmf_orders.order_create_time between ";
        query += QString("'%1-%2-%3'").arg(startYear).arg(startMonth).arg(startDay);
        query += " and ";
        query += QString("'%1-%2-%3'").arg(endYear).arg(endMonth).arg(endDay);
    }
    if(!query.isEmpty()){
        model->setQuery(QString(DEFAULT_SQL_1) + " where "+query + QString(" order by order_create_time desc "));
        lastQuery = QString(DEFAULT_SQL_1) + " where "+query + QString(" order by order_create_time desc ");
        QLOG_INFO()<<"query = "<<QString(DEFAULT_SQL_1) + " where "+query;
    }
}

void ReservationRecord::on_clear_btn_clicked()
{
    model->setQuery(QString(DEFAULT_SQL_1)+ QString(" order by order_create_time desc "));
    lastQuery = DEFAULT_SQL_1+ QString(" order by order_create_time desc ");
}

void ReservationRecord::on_create_table_btn_clicked()
{
    QMap<QString,int> showData;
    QList<QList<QString>> data =  DBManager::getInstance()->select(lastQuery);
    for(auto it : data){
        auto time = it.at(12);
        auto list = time.split("-");
        showData[list.at(0)+"."+list.at(1)]++;//1.1
    }
    signOtherChart("临时表格",showData);
}
